package com.gitee.wsl.text.marker

/**
 * 给文本区间附加一个值
 */
 class ValueMarker(
    textStart: Int,
    textEnd: Int,
    override val name: String?,
    val value: Any?,
    val isMasked: Boolean
) : Marker(textStart, textEnd) {
    constructor(pos: Int, value: Any, masked: Boolean) : this(pos, pos + 1, null, value, masked)

    override fun offset(offset: Int): ValueMarker {
        if (offset == 0) return this
        return newValueMarker(textBegin + offset, textEnd + offset, value)
    }

    protected fun newValueMarker(begin: Int, end: Int, value: Any?): ValueMarker {
        return ValueMarker(begin, end, name, value, isMasked)
    }

    override fun toString(): String {
        val sb = StringBuilder()
        return appendPos(sb).append(':').append(value).toString()
    }

    fun changeValue(value: Any?): ValueMarker {
        return newValueMarker(textBegin, textEnd, value)
    }

    fun changeValue(offset: Int, value: Any): ValueMarker {
        return newValueMarker(textBegin + offset, textEnd + offset, value)
    }

}

fun Marker.Companion.value(textStart: Int, textEnd: Int, value: Any?, masked: Boolean): ValueMarker {
    return ValueMarker(textStart, textEnd, null, value, masked)
}